//
//  HDHubView.m
//  AccountEasy
//
//  Created by 黄志刚 on 2020/8/28.
//  Copyright © 2020 AccountEasy. All rights reserved.
//

#import "HDHubView.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import "HDLoadingView.h"
static CGFloat timeInterVal = 60.0f;
@implementation HDHubView
+ (void)loadingMessage:(NSString *)message {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self lastWindow] animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.tag = 123456789;
        hud.label.text = message;
        hud.label.textColor = colorHex(0xffffff);
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = colorHexA(0x000000, 0.5);
        hud.offset = CGPointMake(0.f, 0.f);
        [hud hideAnimated:YES afterDelay:timeInterVal];
    });
}
+(void)showProess:(CGFloat)progress text:(NSString*)text{
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self lastWindow] animated:YES];
        hud.mode = MBProgressHUDModeDeterminate;
        hud.tag = 123456789;
        hud.label.text = text;
        hud.label.textColor = colorHex(0xffffff);
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = colorHexA(0x000000, 0.8);
        hud.offset = CGPointMake(0.f, 0.f);
        hud.progress = progress;
        
        // [hud hideAnimated:YES afterDelay:timeInterVal];
    });
}
+ (void)showError:(NSString *)error{
    [self showBlackHUDWithMessage: error];
}

+ (void)showMessage:(NSString *)success{
    [self showBlackHUDWithMessage: success];
}
+ (void)showLoadingNoMessage {
    [self showHUDWithMessage:nil];
}

+ (void)showLoading {
    [self showHUDWithMessage:@"加载中..."];
}
+ (void)showLoadingWithTip:(NSString *)tip{
    [self showHUDWithMessage:tip];
}

+ (void)dismissLoading{
    [self dismiss];
}
+ (void)showBlackHUDWithMessage:(NSString *)message {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self lastWindow] animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        hud.bezelView.backgroundColor = colorHexA(0x000000, 0.8);
        hud.label.text = message;
        hud.label.numberOfLines = 0;
        hud.label.textColor = colorHexA(0xffffff, 1);
       [hud hideAnimated:YES afterDelay:timeInterVal];
    });
}
+ (void)showHUDWithImageName:(NSString *)imageName message:(NSString *)message issquare:(BOOL)issquare {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self lastWindow] animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        hud.bezelView.backgroundColor = colorHexA(0xffffff, 0.5);
        UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = issquare;
        hud.label.text = message;
        hud.label.numberOfLines = 0;
        [hud hideAnimated:YES afterDelay:timeInterVal];
    });
}

+ (void)dismiss{
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [[self lastWindow] viewWithTag:123456789];
        [hud hideAnimated:YES];
    });
}

+ (void)showHUDWithMessage:(NSString *)message {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self lastWindow] animated:YES];
        hud.tag = 123456789;
        hud.square = YES;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = [UIColor clearColor];
        hud.mode = MBProgressHUDModeCustomView;
        HDLoadingView *customView = [[HDLoadingView alloc] init];
        customView.message = message;
        hud.customView = customView;
        [hud hideAnimated:YES afterDelay:timeInterVal];
    });
}

+ (UIView *) lastWindow {
    return [UIApplication sharedApplication].keyWindow;
    //return [[UIApplication sharedApplication].windows lastObject];
}

@end
